﻿/* 
 * BtCommLib - the OpenWebNet .NET Library
 *
 * Copyright (C) 2011 BTicino S.p.A.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BTicino.BtCommLib.Attributes;
using BTicino.BtCommLib.Exceptions;
using System.Reflection;
using BTicino.BtCommLib.Domain.Util;
using BTicino.BtCommLib.Trace;

namespace BTicino.BtCommLib.Mappers
{
    public class HighLevelCtxAttr : ICtxAttr
    {

        #region Data members

        private Object _highLevelOpenMsg;
        public Object HighLevelOpenMsg
        {
            get { return _highLevelOpenMsg; }
            set { _highLevelOpenMsg = value; }
        }

        /// <summary>
        /// It contain the list of fields of highLevelOpenMsg
        /// </summary>
        private Dictionary<Type, string> ListFields;

        private static BTLibLogger logger = BTLibLogger.createLogger("HighLevelCtxAttr");

        #endregion

        #region Constructor

        /// <summary>
        /// Create new istance of HighLevelCtxAttr. It rebuild high level message, defined by user, 
        /// into basic BTicino attributes 
        /// </summary>
        /// <param name="highLevelOpenMsg">High level message, defined by user</param>
        public HighLevelCtxAttr(Object highLevelOpenMsg)
        {
            HighLevelOpenMsg = highLevelOpenMsg;
            ListFields = new Dictionary<Type, string>();
            List<FieldInfo> fieldList = BTOpenConverter.ListFieldsOfHierarchy(highLevelOpenMsg.GetType());
            foreach (FieldInfo field in fieldList)
            {
                Who[] pWhoArray = (Who[])field.GetCustomAttributes(typeof(Who), false);
                if (pWhoArray.Length > 0)
                {
                    logger.Debug("[MsgType] TypeName=" + pWhoArray[0].GetType().ToString());
                    ListFields.Add(pWhoArray[0].GetType(), (string)field.GetValue(HighLevelOpenMsg));
                    continue;
                }
                What[] pWhatArray = (What[])field.GetCustomAttributes(typeof(What), false);
                if (pWhatArray.Length > 0)
                {
                    logger.Debug("[MsgType] TypeName=" + pWhatArray[0].GetType().ToString());
                    ListFields.Add(pWhatArray[0].GetType(), (string)field.GetValue(HighLevelOpenMsg));
                    continue;
                }
                Where[] pWhereArray = (Where[])field.GetCustomAttributes(typeof(Where), false);
                if (pWhereArray.Length > 0)
                {
                    logger.Debug("[MsgType] TypeName=" + pWhereArray[0].GetType().ToString());
                    ListFields.Add(pWhereArray[0].GetType(), (string)field.GetValue(HighLevelOpenMsg));
                    continue;
                }
                Dimension[] pDimensionArray = (Dimension[])field.GetCustomAttributes(typeof(Dimension), false);
                if (pDimensionArray.Length > 0)
                {
                    logger.Debug("[MsgType] TypeName=" + pDimensionArray[0].GetType().ToString());
                    ListFields.Add(pDimensionArray[0].GetType(), (string)field.GetValue(HighLevelOpenMsg));
                    continue;
                }
            }
        }

        #endregion

        #region Public methods

        /// <summary>
        /// Retrieve generic BTicino field valule from HighLevelOpenMsg
        /// </summary>
        /// <param name="field">Field name, correspond to annotation field (eg. Who, Where, etc...)</param>
        /// <returns>Field value</returns>
        public string GetCtxAttr(BTAttributeName field)
        {
            switch (field)
            {
                case BTAttributeName.Who:
                    if (ListFields.ContainsKey(typeof(Who)))
                        return ListFields[typeof(Who)];
                    break;
                case BTAttributeName.Where:
                    if (ListFields.ContainsKey(typeof(Where)))
                        return ListFields[typeof(Where)];
                    break;
                case BTAttributeName.What:
                    if (ListFields.ContainsKey(typeof(What)))
                        return ListFields[typeof(What)];
                    break;
                case BTAttributeName.Dimension:
                    if (ListFields.ContainsKey(typeof(Dimension)))
                        return ListFields[typeof(Dimension)];
                    break;
                default:
                    throw new LibException("Field not Found");
            }
            return string.Empty;
        }

        #endregion
    }
}
